/**
 * @file      config_api.c
 * @brief
 * @version   1.0
 * @date      2023.12.14
 *
 * @copyright Copyright (c) 2018 UCchip Technology Co.,Ltd. All rights reserved.
 *
 * Change Logs:
 * Date          Author       Notes
 * 2023.12.14    ucchip       the first version
 *
 */
#include "config_api.h"
#include "config_data.h"
#include <board.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <string.h>

#ifdef PKG_USING_PPP_DEVICE
#include "ppp_device.h"
#endif

#define DBG_ENABLE
#define DBG_SECTION_NAME "config"
#include "uc_log.h"

static backup_data_t g_backup_data;
static const unsigned char g_max_mcs1[2][4] = {{4, 6, 6, 5}, {4, 6, 7, 7}}; // bt0.3
static const unsigned char g_max_mcs2[2][2] = {{7, 5}, {7, 7}};             // bt1.2
static const unsigned char g_init_act_time[4] = {2, 3, 4, 8};

// AT32芯片内部唯一ID，96bit，其中0-16，28-33，79-87共32bit可组合成为4bytes的唯一ID
#define AT32_CHIP_ID_ADDR_0 (0x1FFFF7E8)
#define AT32_CHIP_ID_ADDR_1 (0x1FFFF7EC)
#define AT32_CHIP_ID_ADDR_2 (0x1FFFF7F0)
static unsigned int uc_static_get_chip_id(void)
{
    unsigned int chip_id = 0;
    unsigned int bit = 0;
    unsigned int uid[3] = {0};
    uid[0] = *(uint32_t *)AT32_CHIP_ID_ADDR_0;
    uid[1] = *(uint32_t *)AT32_CHIP_ID_ADDR_1;
    uid[2] = *(uint32_t *)AT32_CHIP_ID_ADDR_2;

    for (int i = 0; i < 96; i++)
    {
        if ((i >= 0 && i <= 16) || (i >= 28 && i <= 33) || (i >= 79 && i <= 87))
        {
            chip_id |= (((uid[i / 32] >> (i % 32)) & 0x01) << bit);
            bit += 1;
        }
    }

    return chip_id;
}

/**
 * @brief  获取网关硬件版本
 *
 * @param  version 网关硬件版本字符串
 * @return uc_ststic_hw_version_e 硬件版本枚举值
 */
#define GW_HW_VERSION_PIN_1 (GET_PIN(F, 11))
#define GW_HW_VERSION_PIN_2 (GET_PIN(F, 12))
#define GW_HW_VERSION_PIN_3 (GET_PIN(F, 15))
#define GW_HW_VERSION_PIN_4 (GET_PIN(F, 14))
static uc_ststic_hw_version_e uc_static_get_hw_version(void)
{
    uc_ststic_hw_version_e hw_version = 0;

    rt_pin_mode(GW_HW_VERSION_PIN_1, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(GW_HW_VERSION_PIN_2, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(GW_HW_VERSION_PIN_3, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(GW_HW_VERSION_PIN_4, PIN_MODE_INPUT_PULLUP);

    rt_thread_mdelay(10);

    hw_version = (uc_ststic_hw_version_e)(rt_pin_read(GW_HW_VERSION_PIN_1) << 3 |
                                          rt_pin_read(GW_HW_VERSION_PIN_2) << 2 |
                                          rt_pin_read(GW_HW_VERSION_PIN_3) << 1 |
                                          rt_pin_read(GW_HW_VERSION_PIN_4));

    return hw_version;
}

static void uc_static_data_set_hw_version(backup_data_t *backup_data)
{
    // 读取硬件版本号
    uc_ststic_hw_version_e hw_version = uc_static_get_hw_version();
    rt_memset(backup_data->static_data.gateway_cfg.base_cfg.hardware_version, 0, sizeof(backup_data->static_data.gateway_cfg.base_cfg.hardware_version));
    switch (hw_version)
    {
    case UC_GW_HW_VERSION_V1_3:
        rt_sprintf(backup_data->static_data.gateway_cfg.base_cfg.hardware_version, CFG_HARDWARE_VERSION_V1_3);
        break;
    case UC_GW_HW_VERSION_V1_5:
        rt_sprintf(backup_data->static_data.gateway_cfg.base_cfg.hardware_version, CFG_HARDWARE_VERSION_V1_5);
        break;
    case UC_GW_HW_VERSION_V1_6:
        rt_sprintf(backup_data->static_data.gateway_cfg.base_cfg.hardware_version, CFG_HARDWARE_VERSION_V1_6);
        break;
    default:
        rt_sprintf(backup_data->static_data.gateway_cfg.base_cfg.hardware_version, CFG_HARDWARE_VERSION_UNKNOWN);
        break;
    }
}

static void uc_static_data_set_default(void)
{
    // 网关软件版本号，恢复默认值
    rt_memcpy(g_backup_data.static_data.gateway_cfg.base_cfg.software_version,
              CFG_SOFTWARE_VERSION,
              sizeof(g_backup_data.static_data.gateway_cfg.base_cfg.software_version));

    uc_static_data_set_hw_version(&g_backup_data);
#if 0
    // wifi mac地址没有设置时，mac 地址： 默认前3个字节 + "dev_id"
    if (0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[0] &&
        0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[1] &&
        0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[2] &&
        0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[3] &&
        0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[4] &&
        0 == g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[5])
    {
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[0] = 0xda;
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[1] = 0xa1;
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[2] = 0x19;
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[3] = 1; // 默认wifi
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[4] = ((g_backup_data.static_data.gateway_cfg.base_cfg.dev_id >> 8) & 0xFF);
        g_backup_data.static_data.gateway_cfg.wifi_cfg.mac[5] = ((g_backup_data.static_data.gateway_cfg.base_cfg.dev_id) & 0xFF);
    }
#endif

#ifdef PKG_USING_PPP_DEVICE
    rt_memset(g_ppp_apn_custom, 0, sizeof(g_ppp_apn_custom));
    rt_snprintf(g_ppp_apn_custom, strlen(PPP_APN_PREFIX) + 1, PPP_APN_PREFIX);
    rt_snprintf(g_ppp_apn_custom + strlen(g_ppp_apn_custom), strlen(g_backup_data.static_data.gateway_cfg.cat1_cfg.apn) + 1, g_backup_data.static_data.gateway_cfg.cat1_cfg.apn);
    rt_snprintf(g_ppp_apn_custom + strlen(g_ppp_apn_custom), strlen(PPP_APN_SUFFIX) + 1, PPP_APN_SUFFIX);
#endif
}

static void uc_static_data_default(void)
{
    backup_data_t backup_data = {
        .data_head = CFG_DATA_HEAD,
        .version = UC_STATIC_VERSION_V1, // default v1.0
        .total_size = 0,
        .used_size = 0,
        .readonly_size = 0,
        .crc32_value = 0,
        .static_data =
            {
                .wiota_cfg = {
                    .sys_cfg = {
                        .symbol_length = UC_SYMBOL_LEN_256,
                        .dlul_ratio = UC_DLUL_RATIO_1_1,
                        .group_number = UC_GROUP_NUM_1,
                        .bt_value = UC_BT_VALUE_03,
                        .subsystem_id = CFG_SUBSYSTEM_ID,
                    },
                    .freq_cfg = {
                        .freq_list = {CFG_DEFA_FREQ1, CFG_DEFA_FREQ2, 255},
                        .current_freq = 255,
                    },
                    .power_cfg = {
                        .power = CFG_TX_POWER,
                    },
                    .rate_cfg = {
                        .unicast_mcs = UC_MCS_2,
                        .mid_mode_state = 0,
                        .high_mode_state = 0,
                        .broadcast_mcs = UC_MCS_2,
                        .crc_type = UC_SWITCH_OFF,
                        .bc_send_round = CFG_BC_SEND_ROUND,
                        .uc_resend_times = CFG_UC_RESEND_TIMES,
                    },
                    .paging_tx_cfg = {
                        .freq = CFG_DEFA_FREQ1,
                        // .spectrum_idx = CFG_SPECTRUM_IDX,
                        // .bandwidth = UC_BT_VALUE_03,
                        .symbol_length = UC_SYMBOL_LEN_256,
                        .awaken_id = CFG_PGAING_TX_AWAKEN_ID,
                        .send_time = CFG_PGAING_TX_SEND_TIME,
                        .mode = UC_SWITCH_OFF,
                    },
                    .sync_paging_cfg = {
                        .dev_id = CFG_SYNC_PGAING_DEV_ID,
                        .detection_period = CFG_SYNC_PGAING_PERIOD,
                        .send_round = CFG_SYNC_PGAING_SEND_ROUND,
                        .continue_fn = CFG_SYNC_PGAING_COUNTINUE_FN,
                    },
                    .ts_cfg = {
                        .ts_mode = UC_TS_NONE,
                        .dev_type = UC_1588_DEV_MASTER,
                        .ts_period = CFG_TS_PERIOD,
                    },
#ifdef WIOTA_SUBF_MODE_SUPPORT
                    .subf_cfg = {
                        .ul_subf_mode = UC_SWITCH_OFF,
                        .block_size = CFG_DL_SUBF_BLOCK_SIZE,
                        .dl_subf_send_round = CFG_DL_SUBF_SEND_ROUND,
                    },
#endif
                },
                .gateway_cfg = {
                    .base_cfg = {
                        .company = CFG_COMPANY,
                        .dev_serial = {0},
                        .dev_id = CFG_DEV_ID,
                        .software_version = CFG_SOFTWARE_VERSION,
                    },
                    .auth_cfg = {
                        .work_mode = UC_NORMAL_MODE,
                        .key_type = UC_KEY_TYPE1,
                        .key = CFG_AUTH_KEY,
                    },
                    .id_mng_cfg = {
                        .assign_start_addr = CFG_ASSIGN_START_ADDR,
                        .assign_end_addr = CFG_ASSIGN_END_ADDR,
                        .addr_num = CFG_ADDR_NUM,
                    },
                    .net_policy_cfg = {
                        .netdev_policy = NETDEV_AUTO,
                        .netdev_name = NETDEV_INTERFACE,
                    },
                    .net_if_cfg = {
                        .netdev_state = NETDEV_INTERFACE,
                        .static_ip = UC_SWITCH_OFF,
                        .mac = {0},
                        .ip_addr = CFG_IP_ADDR,
                        .gateway_addr = CFG_GATEWAY_ADDR,
                        .mask_addr = CFG_MASK_ADDR,
                        .primary_dns = CFG_PRIMARY_DNS,
                        .secondary_dns = CFG_SECONDARY_DNS,
                    },
                    .wifi_cfg = {
                        .netdev_state = NETDEV_WIFI,
                        .static_ip = UC_SWITCH_OFF,
                        .mac = {0},
                        .ip_addr = CFG_IP_ADDR,
                        .gateway_addr = CFG_GATEWAY_ADDR,
                        .mask_addr = CFG_MASK_ADDR,
                        .primary_dns = CFG_PRIMARY_DNS,
                        .secondary_dns = CFG_SECONDARY_DNS,
                        .username = {0},
                        .password = {0},
                    },
                    .cat1_cfg = {
                        .netdev_state = NETDEV_CAT1,
                        .mac = {0},
                        .ip_addr = CFG_IP_ADDR,
                        .gateway_addr = CFG_GATEWAY_ADDR,
                        .mask_addr = CFG_MASK_ADDR,
                        .apn = CFG_PPP_APN,
                    },
                    .bt_cfg = {
                        .mac = {0},
                    },
                    .mqtt_server_cfg = {
                        .url = CFG_MQTT_SERVER_URL, // ucchip server
                        .port = CFG_MQTT_SERVER_PORT,
                        .client_id = CFG_MQTT_CLIENT_ID,
                        .username = {0},
                        .password = {0},
                        .qos = 0, // MQTT QOS0
                    },
                    .data_cfg = {
                        .up_encode = UC_UP_ENCODE_NONE,
                        .ul_data_flow = UC_UP_DATA_FLOW_TO_NET,
                        .ul_data_print = UC_SWITCH_OFF,
                        .dl_data_print = UC_SWITCH_OFF,
                    },
                    .wtd_cfg = {
                        .wtd_state = UC_SWITCH_ON,
                    },
                    .log_cfg = {
                        .ftp_log = UC_SWITCH_OFF,
                        .port = CFG_FTP_PORT,
                    },
                    .log_switch_cfg = {
                        .log_uart = RT_CONSOLE_DEVICE_NAME,
                        .ap_log_output = UC_SWITCH_OFF,
                    },
                    .uart_485_cfg = {
                        .baud_rate = BAUD_RATE_9600,
                    },
                    .timed_cfg = {
                        .time = 0,                       // unit hour
                        .abnormal_time = MQTT_TIMEOUT,   // unit seconds
                        .roll_call_time = UC_SWITCH_OFF, // unit seconds
                        .rc_rto = UC_SWITCH_OFF,
                    },
                },
                .sync_cfg = {
                    .user_def_flag = 0xA, // read only
                    .mode = 0,
                    .symbol_len = 3,
                    .bt = 3,
                    .mcs = 0,
                    .freq = 1,
                    .power = 22,
                    .sync_cycle = 60000,
                    .wiota_log = 1,
                    .watchdog_flag = 1,
                },
                .slot_send_cfg = {
                    .alive_timeout = 0,
                    .data_transport_period = 0, // unit ms
                    .ul_max_len = CFG_TRANSPORT_FRAME_NUM,
                    .dl_max_len = CFG_TRANSPORT_FRAME_NUM,
                    .ul_resend_times = 0,
                    .dl_resend_times = 0,
                    .ul_mcs = 0,
                    .dl_mcs = 0,
                    .bc_frame_send_period = CFG_FN_BROADCAST_CYCLE,
                    .device_access_max = 0,
                    .real_data_transport_period = 0,
                },
            },
    };

    // 读取chip_id用作mac地址和mqtt client_id
    unsigned int chip_id = uc_static_get_chip_id();
    rt_sprintf(backup_data.static_data.gateway_cfg.mqtt_server_cfg.client_id, "client%u", chip_id);

    backup_data.static_data.gateway_cfg.net_if_cfg.mac[0] = 0xda;
    backup_data.static_data.gateway_cfg.net_if_cfg.mac[1] = 0xa1;
    backup_data.static_data.gateway_cfg.net_if_cfg.mac[2] = (chip_id >> 24) & 0xFF;
    backup_data.static_data.gateway_cfg.net_if_cfg.mac[3] = (chip_id >> 16) & 0xFF;
    backup_data.static_data.gateway_cfg.net_if_cfg.mac[4] = (chip_id >> 8) & 0xFF;
    backup_data.static_data.gateway_cfg.net_if_cfg.mac[5] = (chip_id >> 0) & 0xFF;

    uc_static_data_set_hw_version(&backup_data);

    // 设置设备ID为chip_id
    backup_data.static_data.gateway_cfg.base_cfg.dev_id = chip_id;

    // 恢复出厂模式的时候，如果设备ID被修改过，设备ID将不会被恢复到默认值
    if (CFG_DEV_ID != g_backup_data.static_data.gateway_cfg.base_cfg.dev_id && 0 != g_backup_data.static_data.gateway_cfg.base_cfg.dev_id)
    {
        backup_data.static_data.gateway_cfg.base_cfg.dev_id = g_backup_data.static_data.gateway_cfg.base_cfg.dev_id;
    }

    TRACE_I("static data default configuration read ok");

    // 恢复到默认值
    rt_memcpy(&g_backup_data, &backup_data, sizeof(backup_data_t));
    uc_static_active_time_init(UC_SYMBOL_LEN_256);

    // TRACE_W("dev_id %d back dev_id %d", g_backup_data.static_data.gateway_cfg.base_cfg.dev_id, backup_data.static_data.gateway_cfg.base_cfg.dev_id);
}

void uc_static_data_init(void)
{
    int ret = 0;
    // 先赋默认值，再从文件中读取
    uc_static_data_default();
    ret = uc_read_static_data(&g_backup_data.static_data);
    if (0 != ret)
    {
        // ret == 0xff 表示有新增项或某项缺省，只需将默认值加入静态数据，不能将其他数据恢复为默认值
        uc_write_static_data(&g_backup_data.static_data);
    }

    uc_static_data_set_default();

    return;
}

// 恢复出厂默认值
void uc_static_data_reset(void)
{
    uc_static_data_default();
    TRACE_W("restore the factory default configuration");
    uc_write_static_data(&g_backup_data.static_data);
    TRACE_I("write default value to config file\n");
}

short uc_static_get_version(void)
{
    return g_backup_data.version;
}

void uc_static_save_op(void)
{
    uc_write_static_data(&g_backup_data.static_data);
}

void uc_static_active_time_init(unsigned char symbol_length)
{
    uc_static_get_wiota_act_cfg()->active_time = g_init_act_time[symbol_length];
    TRACE_I("active time init %d", g_init_act_time[symbol_length]);
}

wiota_cfg_t *uc_static_get_wiota_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg;
}

wiota_sys_cfg_t *uc_static_get_wiota_sys_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.sys_cfg;
}

void uc_static_set_wiota_sys_cfg(wiota_sys_cfg_t *sys_cfg)
{
    rt_memcpy(uc_static_get_wiota_sys_cfg(), sys_cfg, sizeof(wiota_sys_cfg_t));
}

wiota_act_cfg_t *uc_static_get_wiota_act_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.act_cfg;
}

void uc_static_set_wiota_act_cfg(wiota_act_cfg_t *act_cfg)
{
    rt_memcpy(uc_static_get_wiota_act_cfg(), act_cfg, sizeof(wiota_act_cfg_t));
}

wiota_freq_cfg_t *uc_static_get_wiota_freq_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.freq_cfg;
}

void uc_static_set_wiota_freq_cfg(wiota_freq_cfg_t *freq_cfg)
{
    rt_memcpy(uc_static_get_wiota_freq_cfg(), freq_cfg, sizeof(wiota_freq_cfg_t));
}

wiota_power_cfg_t *uc_static_get_wiota_power_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.power_cfg;
}

void uc_static_set_wiota_power_cfg(wiota_power_cfg_t *power_cfg)
{
    rt_memcpy(uc_static_get_wiota_power_cfg(), power_cfg, sizeof(wiota_power_cfg_t));
}

wiota_rate_cfg_t *uc_static_get_wiota_rate_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.rate_cfg;
}

void uc_static_set_wiota_rate_cfg(wiota_rate_cfg_t *rate_cfg)
{
    rt_memcpy(uc_static_get_wiota_rate_cfg(), rate_cfg, sizeof(wiota_rate_cfg_t));
}

unsigned char uc_static_get_max_mcs(unsigned char type)
{
    wiota_sys_cfg_t *sys_cfg = uc_static_get_wiota_sys_cfg();

    if (sys_cfg->bt_value == UC_BT_VALUE_03)
    {
        return g_max_mcs1[type][sys_cfg->symbol_length];
    }
    else
    {
        return g_max_mcs2[type][sys_cfg->symbol_length];
    }
}

wiota_paging_tx_cfg_t *uc_static_get_wiota_paging_tx_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.paging_tx_cfg;
}

void uc_static_set_wiota_paging_tx_cfg(wiota_paging_tx_cfg_t *paging_tx_cfg)
{
    rt_memcpy(uc_static_get_wiota_paging_tx_cfg(), paging_tx_cfg, sizeof(wiota_paging_tx_cfg_t));
}

wiota_sync_paging_cfg_t *uc_static_get_wiota_sync_paging_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.sync_paging_cfg;
}

void uc_static_set_wiota_sync_paging_cfg(wiota_sync_paging_cfg_t *sync_paging_cfg)
{
    rt_memcpy(uc_static_get_wiota_sync_paging_cfg(), sync_paging_cfg, sizeof(wiota_sync_paging_cfg_t));
}

wiota_ts_cfg_t *uc_static_get_wiota_ts_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.ts_cfg;
}

void uc_static_set_wiota_ts_cfg(wiota_ts_cfg_t *ts_cfg)
{
    rt_memcpy(uc_static_get_wiota_ts_cfg(), ts_cfg, sizeof(wiota_ts_cfg_t));
}

#ifdef WIOTA_SUBF_MODE_SUPPORT
wiota_subf_mode_cfg_t *uc_static_get_wiota_subf_mode_cfg(void)
{
    return &g_backup_data.static_data.wiota_cfg.subf_cfg;
}

void uc_static_set_wiota_subf_mode_cfg(wiota_subf_mode_cfg_t *subf_cfg)
{
    rt_memcpy(uc_static_get_wiota_subf_mode_cfg(), subf_cfg, sizeof(wiota_subf_mode_cfg_t));
}
#endif

gateway_cfg_t *uc_static_get_gateway_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg;
}

gw_base_cfg_t *uc_static_get_gateway_base_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.base_cfg;
}

void uc_static_set_gateway_base_cfg(gw_base_cfg_t *base_cfg)
{
    rt_memcpy(uc_static_get_gateway_base_cfg(), base_cfg, sizeof(gw_base_cfg_t));
}

gw_id_manager_cfg_t *uc_static_get_gateway_id_manager_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.id_mng_cfg;
}

void uc_static_set_gateway_id_manager_cfg(gw_id_manager_cfg_t *id_mng_cfg)
{
    rt_memcpy(uc_static_get_gateway_id_manager_cfg(), id_mng_cfg, sizeof(gw_id_manager_cfg_t));
}

gw_auth_cfg_t *uc_static_get_gateway_auth_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.auth_cfg;
}

void uc_static_set_gateway_auth_cfg(gw_auth_cfg_t *auth_cfg)
{
    rt_memcpy(uc_static_get_gateway_auth_cfg(), auth_cfg, sizeof(gw_auth_cfg_t));
}

gw_net_policy_cfg_t *uc_static_get_gateway_net_policy_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.net_policy_cfg;
}

void uc_static_set_gateway_net_policy_cfg(gw_net_policy_cfg_t *net_policy_cfg)
{
    rt_memcpy(uc_static_get_gateway_net_policy_cfg(), net_policy_cfg, sizeof(gw_net_policy_cfg_t));
}

gw_net_if_cfg_t *uc_static_get_gateway_net_if_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.net_if_cfg;
}

void uc_static_set_gateway_net_if_cfg(gw_net_if_cfg_t *net_if_cfg)
{
    rt_memcpy(uc_static_get_gateway_net_if_cfg(), net_if_cfg, sizeof(gw_net_if_cfg_t));
}

gw_wifi_cfg_t *uc_static_get_gateway_wifi_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.wifi_cfg;
}

void uc_static_set_gateway_wifi_cfg(gw_wifi_cfg_t *net_wifi_cfg)
{
    rt_memcpy(uc_static_get_gateway_wifi_cfg(), net_wifi_cfg, sizeof(gw_wifi_cfg_t));
}

gw_cat1_cfg_t *uc_static_get_gateway_cat1_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.cat1_cfg;
}

void uc_static_set_gateway_cat1_cfg(gw_cat1_cfg_t *cat1_cfg)
{
    rt_memcpy(uc_static_get_gateway_cat1_cfg(), cat1_cfg, sizeof(gw_cat1_cfg_t));
}

gw_bt_cfg_t *uc_static_get_gateway_bt_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.bt_cfg;
}

void uc_static_set_gateway_bt_cfg(gw_bt_cfg_t *bt_cfg)
{
    rt_memcpy(uc_static_get_gateway_bt_cfg(), bt_cfg, sizeof(gw_bt_cfg_t));
}

gw_mqtt_server_cfg_t *uc_static_get_gateway_mqtt_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.mqtt_server_cfg;
}

void uc_static_set_gateway_mqtt_cfg(gw_mqtt_server_cfg_t *mqtt_server_cfg)
{
    rt_memcpy(uc_static_get_gateway_mqtt_cfg(), mqtt_server_cfg, sizeof(gw_mqtt_server_cfg_t));
}

gw_data_cfg_t *uc_static_get_gateway_data_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.data_cfg;
}

void uc_static_set_gateway_data_cfg(gw_data_cfg_t *data_cfg)
{
    rt_memcpy(uc_static_get_gateway_data_cfg(), data_cfg, sizeof(gw_data_cfg_t));
}

gw_wtd_cfg_t *uc_static_get_gateway_wtd_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.wtd_cfg;
}

void uc_static_set_gateway_wtd_cfg(gw_wtd_cfg_t *wtd_cfg)
{
    rt_memcpy(uc_static_get_gateway_wtd_cfg(), wtd_cfg, sizeof(gw_wtd_cfg_t));
}

gw_ftp_log_cfg_t *uc_static_get_gateway_log_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.log_cfg;
}

void uc_static_set_gateway_log_cfg(gw_ftp_log_cfg_t *log_cfg)
{
    rt_memcpy(uc_static_get_gateway_log_cfg(), log_cfg, sizeof(gw_ftp_log_cfg_t));
}

gw_log_switch_cfg_t *uc_static_get_gateway_log_switch_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.log_switch_cfg;
}

void uc_static_set_gateway_log_switch_cfg(gw_log_switch_cfg_t *log_switch_cfg)
{
    rt_memcpy(uc_static_get_gateway_log_switch_cfg(), log_switch_cfg, sizeof(gw_log_switch_cfg_t));
}

gw_uart_485_cfg_t *uc_static_get_gateway_uart_485_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.uart_485_cfg;
}

void uc_static_set_gateway_uart_485_cfg(gw_uart_485_cfg_t *uart_485_cfg)
{
    rt_memcpy(uc_static_get_gateway_uart_485_cfg(), uart_485_cfg, sizeof(gw_uart_485_cfg_t));
}

gw_timed_cfg_t *uc_static_get_gateway_timed_cfg(void)
{
    return &g_backup_data.static_data.gateway_cfg.timed_cfg;
}

void uc_static_set_gateway_timed_cfg(gw_timed_cfg_t *timed_cfg)
{
    rt_memcpy(uc_static_get_gateway_timed_cfg(), timed_cfg, sizeof(gw_timed_cfg_t));
}

sync_cfg_t *uc_static_get_sync_cfg(void)
{
    return &g_backup_data.static_data.sync_cfg;
}

void uc_static_set_sync_cfg(sync_cfg_t *sync_cfg)
{
    rt_memcpy(uc_static_get_sync_cfg(), sync_cfg, sizeof(sync_cfg_t));
}

slot_send_cfg_t *uc_static_get_slot_send_cfg(void)
{
    return &g_backup_data.static_data.slot_send_cfg;
}

void uc_static_set_slot_send_cfg(slot_send_cfg_t *slot_send_cfg)
{
    rt_memcpy(uc_static_get_slot_send_cfg(), slot_send_cfg, sizeof(slot_send_cfg_t));
}

static void get_device_id(int argc, char **argv)
{
    if (argc != 1)
    {
        TRACE_E("get device id argc error");
        return;
    }

    unsigned int device_id = g_backup_data.static_data.gateway_cfg.base_cfg.dev_id;
    TRACE_I("gateway device id: 0x%08x, %u", device_id, device_id);
}
MSH_CMD_EXPORT_ALIAS(get_device_id, device_id, get device id);
